package com.bkgy.controller.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.controller.accident.entity.TAccident;
import com.bkgy.controller.accident.entity.dto.TAccidentDto;
import com.bkgy.controller.accident.entity.dto.TAccidentRes;
import com.bkgy.controller.accident.mapper.IAccidentMapper;
import com.bkgy.controller.accident.service.IAccidentService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TAccidentServiceImpl extends ServiceImpl<IAccidentMapper, TAccident> implements IAccidentService {
    @Override
    public ResultData<String> saveAccident(TAccident tech) {
        return ResultData.ResultDataSuccess(save(tech) ? "success" : "failure");
    }

    @Override
    public ResultData<String> updateAccident(TAccident tech) {
        return ResultData.ResultDataSuccess(updateById(tech) ? "success" : "failure");
    }

    @Override
    public ResultData<Page<TAccident>> pageTech(QueryDTO<TAccidentDto> queryDTO) {
        Page<TAccident> p = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
        LambdaQueryWrapper<TAccident> wrapper = new LambdaQueryWrapper();
        if (!StringUtils.isEmpty(queryDTO.getDto().getCompleteStatus())) {
            if ("未完成".equals(queryDTO.getDto().getCompleteStatus())) {
                wrapper.ne(TAccident::getCompleteStatus, "已完成");
            } else {
                wrapper.eq(TAccident::getCompleteStatus, queryDTO.getDto().getCompleteStatus());
            }

        }
        wrapper.orderByDesc(TAccident::getId);
        Page<TAccident> result = page(p, wrapper);
        List<TAccident> list = result.getRecords();
        for (TAccident acc : list) {
            Calendar c1 = Calendar.getInstance();
            c1.setTime(DateUtil.getStringToDate("yyyy-MM-dd", acc.getEstResoTime()));
            long difference = new Date().getTime() - c1.getTimeInMillis();
            long day = difference / (3600 * 24 * 1000);
            if (day > 5) {
                acc.setStatus("超时");
            }
        }
        return ResultData.ResultDataSuccess(list, (int) result.getTotal());
    }

    @Override
    public ResultData<String> deleteAccident(Integer indocno) {
        return ResultData.ResultDataSuccess(removeById(indocno) ? "success" : "failure");
    }

    @Override
    public ResultData importantPoint(String beginDate, String endDate) {
        LambdaQueryWrapper<TAccident> w = new LambdaQueryWrapper();
        w.ne(TAccident::getCompleteStatus, "已完成").orderByDesc(TAccident::getEstResoTime);
        List<TAccident> list = list(w);

        List<TAccidentRes> resList = new ArrayList<>();
        for (TAccident acc : list) {
            TAccidentRes res = new TAccidentRes();
            res.setTitle(acc.getProblem());
            res.setMain1(acc.getEqpFeedback());
            res.setMain2(acc.getCollaborate());
            resList.add(res);
        }
        return ResultData.ResultDataSuccess(resList, resList.size());
    }
}
